<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" :labelWidth="100" :collapse="false" @onCollapse="collapseHandle" @filterChange="handleFilterChange" />
    <FilterTable
      ref="mainTable"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :columns="tableColumns"
      columnsRef="partInventoryInfo"
      :datakey="'rows'"
      selectionType="single"
      :onRowSelectChange="handleRowSelectChange"
      :onColumnsChange="columns => (this.tableColumns = columns)"
    >
      <template slot="controls">
        <el-button size="small" type="primary" @click="exportExcel">导出</el-button>
        <el-button size="small" type="primary" icon="el-icon-printer" :disabled="prindfBtnDisabled" @click="handleDayin">打印</el-button>
      </template>
    </FilterTable>
    <div>
      <BaseDialog width="80%" title="入库单明细" :visible.sync="isShowpartBuyItems" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <partBuyItems :top="top" @cancel="isShowpartBuyItems = false" />
      </BaseDialog>
      <BasePrint ref="Salesprint" :data="SalesprintList" template="templatepartInventory" />
    </div>
  </div>
</template>

<script>
import { queryPartBuy } from '@/api/part/report/billSearch';
import { printData, getDealerUser, exportData } from '@/api/part/partInventory/partInventory/index.js';
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils/index';
import { Checkbox } from 'element-ui';
import partBuyItems from './partBuyItems';

export default {
  name: 'partInventoryInfo',
  mixins: [dictionary],
  components: {
    partBuyItems
  },
  data() {
    return {
      top: {},
      SalesprintList: {},
      cunUserList: [],
      cunPrintid: '',
      topFilterList: this.createTopFilterList(),
      tableColumns: this.createTableColumns(),
      isShowpartBuyItems: false,
      prindfBtnDisabled: true,
      fetchapi: queryPartBuy,
      fetchParams: { xhrAbort: true }
    };
  },
  mounted() {
    this.getDealerUser();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '入库单号',
          fieldName: 'stockInNo',
          placeholder: '请输入...'
        },
        {
          type: 'RANGE_DATE',
          fieldName: 'startDFinish|endDFinish',
          label: '入库日期',
          initialValue: [],
          dateType: 'daterange'
        },
        {
          type: 'RANGE_DATE',
          fieldName: 'startDCreate|endDCreate',
          label: '开单日期',
          initialValue: [],
          dateType: 'daterange'
        },
        {
          type: 'INPUT',
          label: '业务往来客户',
          fieldName: 'customerName',
          placeholder: '请输入...'
        },
        {
          type: 'SELECT',
          label: '入库类型',
          fieldName: 'stockInType',
          itemList: this.createDictList(8088),
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo',
          placeholder: '请输入...'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          // sorter: true,
          width: 70,
          dataIndex: 'xuhao',
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          },
          sorter: true
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: params => {
            return (
              <div>
                <el-button type="text" size="mini" onClick={() => this.showItems(params.row)}>
                  详情
                </el-button>
              </div>
            );
          }
        },
        {
          title: '入库单号',
          dataIndex: 'stockInNo',
          filter: true,
          sorter: true,
          filterType: 'input'
        },
        {
          title: '货运单号',
          dataIndex: 'purchaseCode',
          filter: true,
          sorter: true,
          filterType: 'input'
        },
        {
          title: '业务往来客户',
          dataIndex: 'customerName',
          filter: true,
          sorter: true,
          filterType: 'input'
        },
        {
          title: '入库数量',
          sorter: true,
          dataIndex: 'inQuantity',
          precision: 2,
          filter: true,
          filterType: 'number',
          min: 0,
          max: Infinity,
          widht: 130
        },
        {
          title: '入库日期',
          dataIndex: 'finishedDate',
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          sorter: true,
          filterType: 'date-range'
        },
        {
          title: '开单日期',
          dataIndex: 'createdAt',
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          sorter: true,
          filterType: 'date-range'
        },
        {
          title: '不含税总金额',
          sorter: true,
          dataIndex: 'inAmount',
          precision: 2,
          filter: true,
          filterType: 'number',
          min: 0,
          max: Infinity,
          widht: 130
        },
        {
          title: '含税总金额',
          sorter: true,
          dataIndex: 'inAmountTaxed',
          precision: 2,
          filter: true,
          filterType: 'number',
          min: 0,
          max: Infinity,
          widht: 130
        },
        {
          title: '入库类型',
          dataIndex: 'stockInType',
          filter: true,
          sorter: true,
          filterItems: this.createDictList(8088),
          dictItems: this.createDictList(8088),
          filterType: 'checkbox'
        },
        {
          title: '入库状态',
          dataIndex: 'isFinished',
          filter: true,
          sorter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          dictItems: this.createDictList('1004')
        }
      ];
    },
    collapseHandle() {},
    handleFilterChange(e) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign(this.fetchParams, {
        ...e,
        xhrAbort: false,
        startDCreate: e.startDCreate ? e.startDCreate.split(' ')[0] : '',
        endDCreate: e.endDCreate ? e.endDCreate.split(' ')[0] : '',
        startDFinish: e.startDFinish ? e.startDFinish.split(' ')[0] : '',
        endDFinish: e.endDFinish ? e.endDFinish.split(' ')[0] : ''
      });
    },
    // 打印
    async handleDayin() {
      // console.log(this.printdataList, 'this.printdataList');
      this.dy = true;
      let printParams = '';
      // 获取打印参数
      // try {
      //   printParams = await queryPartinventoryPrintParam();
      // } catch (err) {
      //   console.log('获取入库打印参数失败', err);
      // }
      const res = await printData(this.cunPrintid, printParams ? printParams.data[0].bParaValue : 0);
      this.SalesprintList = res.data;
      this.SalesprintList.baseInfo.STOCK_IN_TYPE = '采购入库-' + this.createDictText(this.SalesprintList.baseInfo.STOCK_IN_TYPE, '8088'); // 修改出入库类型为采购入库
      // this.SalesprintList.baseInfo.STOCK_IN_TYPE = ;
      this.SalesprintList.baseInfo.FINISHED_TYPE = '入库时间'; // 修改领料时间为->入库时间
      this.cunUserList.forEach(x => {
        if (this.SalesprintList.baseInfo.USER_ID == x.userId) {
          this.SalesprintList.baseInfo.USER_NAME = x.employeeName;
        }
        if (this.SalesprintList.baseInfo.CREATED_BY == x.userId) {
          this.SalesprintList.baseInfo.CREATED_NAME = x.employeeName;
        }
      });
      // this.SalesprintList.baseInfo.USER_ID
      // 打印
      this.$nextTick(() => {
        this.$refs.Salesprint.EXCUTE_PRINT();
      });
    },
    async exportExcel() {
      let params = await this.$refs.topFilter.GET_FORM_DATA();
      console.log(params, 'params::');
      params[1].startDCreate = params[1].startDCreate ? params[1].startDCreate.split(' ')[0] : '';
      params[1].endDCreate = params[1].endDCreate ? params[1].endDCreate.split(' ')[0] : '';
      params[1].startDFinish = params[1].startDFinish ? params[1].startDFinish.split(' ')[0] : '';
      params[1].endDFinish = params[1].endDFinish ? params[1].endDFinish.split(' ')[0] : '';
      let res = await exportData(params[1]);
      exportExcelFile(res.data, '采购入库');
    },
    async getDealerUser() {
      const res = await getDealerUser();
      this.cunUserList = res.data;
    },
    handleRowSelectChange(rows) {
      console.log(rows, 'rows::');
      if (rows.length > 0 && rows[0].isFinished == '10041001') {
        this.prindfBtnDisabled = false;
        this.cunPrintid = rows[0].partBuyId;
        console.log('printable');
      } else {
        this.prindfBtnDisabled = true;
        this.cunPrintid = '';
      }
      console.log(this.cunPrintid, 'this.cunPrintid::');
    },
    showItems(e) {
      this.isShowpartBuyItems = true;
      this.top = e;
    }
  }
};
</script>
